﻿using System;
using System.Windows.Forms;
using LDSModel.Dal;
using LDSProject.Properties;

namespace LDSProject.Forms
{
    public partial class UserModifyForm : Form
    {
        readonly UserManageForm _manageForm;

        public UserModifyForm(UserManageForm form)
        {
            InitializeComponent();
            _manageForm = form;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var userName = tbUserName.Text.Trim();

            if (string.IsNullOrWhiteSpace(userName))
            {
                tbUserName.Focus();
                tbUserName.SelectAll();
                lbInfo.Text = Resources.UserNameIsNull;
                lbInfo.Visible = true;
                return;
            }

            var result = UserService.AddUser(userName, userName);

            if (!result.Success)
            {
                lbInfo.Text = result.Code.ToMessage();
                lbInfo.Visible = true;
                return;
            }

            _manageForm.btnQuery.PerformClick();

            MessageBox.Show(this, string.Format("添加用户[{0}]成功!", userName));
        }

        private void tbUserName_TextChanged(object sender, EventArgs e)
        {
            lbInfo.Visible = false;
        }
    }
}
